﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.Web.Mvc.Paging;
using Kooboo.Commerce.Contexts.Brands;
using Kooboo.Web.Mvc;




namespace Kooboo.Web.Controllers
{
    using Kooboo.Commerce;
    using Kooboo.Commerce.Services;
    public class BrandController : ControllerBase
    {
        //
        // GET: /Brand/

        public ActionResult Index(int id, int? page, string sort)
        {
            this.BindViewData();

            BindData(id);

            ProductSortType sortType = ProductSortType.NameByAsc;

            switch (sort)
            {
                case "nameza":
                    sortType = ProductSortType.NameByDesc;
                    break;
                case "pricelh":
                    sortType = ProductSortType.PriceByAsc;
                    break;
                case "pricehl":
                    sortType = ProductSortType.PriceByDesc;
                    break;
                default:
                    sortType = ProductSortType.NameByAsc;
                    break;
            }

            var service = new ProductService();

            var products = service.GetProductsByBrandId(id, sortType, page ?? 1, Configuration.PageSize)
                .Select(i => new
                {
                    Id = i.ProductId,
                    i.Name,
                    Price = i.Price.ToString("C"),
                    Image = combianThumbnailImageUrl(i.Thumbnail),// Kooboo.Commerce.Configuration.FindImageUrlBase(Kooboo.Commerce.Configuration.ProductImageFolder, "Thumnail") + i.Thumbnail,
                    Url = this.Url.Action<ProductController>(m => m.View(i.ProductId, null))
                })
                .AsEnumerable();


            return View(products);
        }

        private void BindData(int id)
        {
            var brand = this.Actor.QueryBrands().FirstOrDefault(i => i.Id == id);

            this.Select(brand);
        }

        private string combianThumbnailImageUrl(string thumbnailName)
        {
            if (!string.IsNullOrEmpty(thumbnailName))
            {
                return Kooboo.Commerce.Configuration.FindImageUrlBase(Kooboo.Commerce.Configuration.ProductImageFolder, "Thumnail") + thumbnailName;
            }
            else
            {
                return Configuration.ImageNotAvailable_Thumbnail;
            }
        }

    }
}
